package com.yugao.fintech.draper.admin.controller;

import com.yugao.fintech.draper.admin.service.RoleService;
import com.yugao.fintech.draper.admin.param.RoleDTO;
import com.yugao.fintech.draper.core.model.ResultVO;
import com.yugao.fintech.draper.security.SecurityUser;
import com.yugao.fintech.draper.security.util.SecurityUserUtil;
import com.yugao.fintech.draper.core.model.DeptDataScopeDTO;
import com.yugao.fintech.draper.datasource.entity.PageResult;
import com.yugao.fintech.draper.admin.param.RoleQuery;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@Tag(name = "系统角色管理")
@RequiredArgsConstructor
public class RoleController {
	private final RoleService roleService;

	@Operation(summary = "获取所有角色")
	@GetMapping("/listAll")
	public ResultVO<List<RoleDTO>> findAll() {
		return ResultVO.success(roleService.listAll());
	}

	@Operation(summary = "查询角色信息列表分页")
	@GetMapping("/page")
	public ResultVO<PageResult<RoleDTO>> pageQuery(RoleQuery roleQuery) {
		return ResultVO.success(PageResult.success(roleService.pageQuery(roleQuery)));
	}

	@Operation(summary = "添加角色")
	@PostMapping("/addRole")
	public ResultVO<RoleDTO> add(@RequestBody RoleDTO roleDTO) {
		return ResultVO.success(roleService.addRole(roleDTO));
	}


	@Operation(summary = "修改角色信息")
	@PutMapping("/editRole")
	public ResultVO<Boolean> edit(@RequestBody RoleDTO roleDTO) {
		return ResultVO.success(roleService.editRole(roleDTO));
	}

	@Operation(summary = "根据id获取角色信息")
	@GetMapping("/getRoleById/{id}")
	public ResultVO<RoleDTO> findById(@PathVariable("id") Long id) {
		return ResultVO.success(roleService.getRoleById(id));
	}

	@Operation(summary = "删除角色信息")
	@DeleteMapping("/deleteByRoleId/{id}")
	public ResultVO<Boolean> deleteByRoleId(@PathVariable("id") Long id) {
		return ResultVO.success(roleService.deleteByRoleId(id));
	}

	@Operation(summary = "获取当前用户角色下的数据权限")
	@GetMapping("/getDataPermission")
	public ResultVO<DeptDataScopeDTO> getDataPermission() {
		SecurityUser user = SecurityUserUtil.getCurrentUser();
		return ResultVO.success(roleService.getDataPermission(user));
	}
}
